package com.dgut.legalservice.dao;

import com.dgut.legalservice.domain.Order;
import com.dgut.legalservice.domain.OrderPr;
import com.dgut.legalservice.domain.OrderVo;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.jdbc.SQL;
import org.apache.ibatis.mapping.FetchType;
import org.springframework.context.annotation.Lazy;

import java.util.List;

@Mapper
public interface IOrderDao extends tk.mybatis.mapper.common.Mapper<Order> {

    /**
     * 添加订单
     * @param order
     * @return
     */
    @Insert("insert into t_order(orderNum,orderName,serviceType,allAmount,sendOrderStatus,status,acceptStatus," +
            "createTime,updateTime,createBy,updateBy,flowStatus,payNum,payStatus,nextStatus,acceptUserId,userId) values " +
            "(#{orderNum},#{orderName},#{serviceType},#{allAmount},#{sendOrderStatus},#{status},#{acceptStatus}," +
            "#{createTime},#{updateTime},#{createBy},#{updateBy},#{flowStatus},#{payNum},#{payStatus},#{nextStatus}," +
            "#{acceptUserId},#{userId})")
    @SelectKey(keyColumn="id",keyProperty="id",resultType=Integer.class,before =
            false, statement = {"select last_insert_id()"})
    public int addOrder(Order order);

    /**
     * 添加产品实例关联表
     * @return
     */
    @Insert("insert into t_product_real(orderId,productId) values (#{orderId},#{productId})")
    @SelectKey(keyColumn="id",keyProperty="id",resultType=Integer.class,before =
            false, statement = {"select last_insert_id()"})
    public int addOrderRer(OrderPr orderPr);

    /**
     * 对相应的实例表执行第二次更新操作,就是添加公司的id
     * @param id
     * @return
     */
    @Update("update t_product_real set companyId = #{companyId} where id = #{id} ")
    @SelectKey(keyColumn="id",keyProperty="id",resultType=Integer.class,before =
            false, statement = {"select last_insert_id()"})
    public int addOrderRer1(Integer id,Integer companyId);

    /**
     * 订单产品多对多
     * @return
     */
    @SelectProvider(type = OrderPro.class,method = "selectOrder")
    @Results(id = "orderMap",value = {
            @Result(id = true, column = "id", property = "id"),
            @Result(column = "orderNum", property = "orderNum"),
            @Result(column = "orderName", property = "orderName"),
            @Result(column = "productName"),
            @Result(column = "serviceType",property = "serviceType"),
            @Result(column = "allAmount",property = "allAmount"),
            @Result(column = "sendOrderStatus",property = "sendOrderStatus"),
            @Result(column = "status",property = "status"),
            @Result(column = "acceptStatus",property = "acceptStatus"),
            @Result(column = "createTime",property = "createTime"),
            @Result(column = "updateTime",property = "updateTime"),
            @Result(column = "createBy",property = "createBy"),
            @Result(column = "updateBy", property = "updateBy"),
            @Result(column = "flowStatus",property = "flowStatus"),
            @Result(column = "payNum",property = "payNum"),
            @Result(column = "payStatus",property = "payStatus"),
            @Result(column = "nextStatus",property = "nextStatus"),
            @Result(column = "acceptUserId",property = "acceptUserId"),
            @Result(column = "{id=id,productName=productName}",property = "products",
                           many = @Many(select = "com.dgut.legalservice.dao.IProduct.findProductById",fetchType = FetchType.EAGER)),
            @Result(column = "id",property = "companies",
            many = @Many(select = "com.dgut.legalservice.dao.ICompanyDao.findCompanyById",fetchType =FetchType.EAGER )),
            @Result(column = "id",property = "approval",
            one = @One(select = "com.dgut.legalservice.dao.IApproval.findApprovalById",fetchType = FetchType.EAGER)),
    })
    public List<Order> selectOrder(OrderVo orderVo);

    class OrderPro{
        public String selectOrder(OrderVo orderVo){
            return new SQL(){{
                SELECT("*");
                SELECT("#{productName} as productName");
                FROM("t_order");
                if(orderVo.getUserId()!=null && "普通用户".equals(orderVo.getType())){ //普通用户查询订单
                    WHERE("userId = #{userId}");
                }
                if (orderVo.getUserId()!=null && "服务商".equals(orderVo.getType()) && "抢单".equals(orderVo.getStatus())){//服务商抢单的订单
                    WHERE("userId = #{userId}");
                    WHERE("status = #{status}");
                }
                if (orderVo.getStatus()!=null ){//根据流程状态查询订单 查询服务商和平台方的状态
                    WHERE("status = #{status}");
                    if (orderVo.getOrderNum() != null) {
                        WHERE("orderNum like #{orderNum}");
                    }
                }
            }}.toString();
        }
    }

    /**
     * 更新支付状态
     * @param id
     * @param payStatus
     * @return
     */
    @Update("update t_order set payStatus = '${payStatus}' where id = ${id}")
    public int payFir(Integer id,String payStatus);
}
